package org.benben.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.benben.shortlink.admin.common.convention.result.Result;
import org.benben.shortlink.admin.common.convention.result.Results;
import org.benben.shortlink.admin.dto.req.ShortLinkGroupSaveReqDTO;
import org.benben.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import org.benben.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import org.benben.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import org.benben.shortlink.admin.service.GroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 *
 * @author benben
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin")
public class GroupController {

    private final GroupService groupService;

    /**
     * 新增分组
     */
    @PostMapping("/v1/group")
    public Result<Void> save(@RequestBody ShortLinkGroupSaveReqDTO requestParam) {
        groupService.saveGroup(requestParam.getName());
        return Results.success();
    }

    /**
     * 查询用户短链接分组集合
     */
    @GetMapping("/v1/group")
    public Result<List<ShortLinkGroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }

    /**
     * 更新分组信息
     */
    @PutMapping("/v1/group")
    public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO requestParam) {
        groupService.updateGroup(requestParam);
        return Results.success();
    }

    /**
     * 删除分组信息
     */
    @DeleteMapping("/v1/group")
    public Result<Void> deleteGroup(@RequestParam("gid")String gid) {
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     * 设置分组排序信息
     */
    @PostMapping("/v1/group/sort")
    public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> requestParam) {
        groupService.sortGroup(requestParam);
        return Results.success();
    }

}
